<template>
  <el-container class="container">
    <el-header class="home-header">
      <div class="header-title">运营办公系统</div>
      <div class="userinfo">
        <el-dropdown @command="handleCommand">
          <div style="display: flex;flex-direction:column;justify-content: center;align-items: center">
            <el-avatar
              :size="45"
              :src="userinfo.userFace"
              @error="errorHandler"
              alt="用户头像"
            >
              <img
                src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"
                alt="空白头像"
              />
            </el-avatar>
            <span
              class="username"
              style="font-size:0.9rem;color:#FFF;margin-top: 6px"
            >
              {{ userinfo.name }}
            </span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="info">个人中心</el-dropdown-item>
              <el-dropdown-item command="setting">设置</el-dropdown-item>
              <el-dropdown-item command="logout">注销登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>

        </el-dropdown>

      </div>
    </el-header>
    <!-- 首页主体部分 -->
    <el-container>
      <!-- 侧边导航 -->
      <el-aside
        class="aside" style="width:205px"
      >
        <el-menu
          router
          unique-opened
        >
          <template   v-for="(item,index) in routes">
          <el-submenu
            :index="index + ''"
              v-if="item.name!='所有'"
            :key="index"
          >
            <template #title>
              <i
                :class="item.iconCls?item.iconCls:'el-icon-setting'"
                style="margin-right: 10px;color: #333"
              ></i>
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item
              v-for="(children,index2) in item.children"
              :index="children.path"
              :key="index2"
            >
              <i
                :class="children.iconCls?children.iconCls:'el-icon-setting'"
                style="margin-right: 10px;color: #333"
              ></i>
              <span>{{ children.name }} </span>
            </el-menu-item>
          </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <!-- 主体功能区 -->
      <el-main>
        <el-breadcrumb
          separator-class="el-icon-arrow-right"
          v-if="this.$route.path !== '/home'"
        >
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: this.$route.path }">{{ this.$route.name }}</el-breadcrumb-item>
        </el-breadcrumb>
        <div
          class="home-welcome"
          v-else
        >欢迎来到运营办公系统</div>
        <router-view class="home-router-view" />
      </el-main>
    </el-container>

  </el-container>
</template>

<script>
import { postRequest } from "@/utils/api";
import { ElMessage, ElMessageBox } from "element-plus";
import store from "../store";
import router from "../router";
import { reactive, toRefs,computed } from '@vue/reactivity';

export default {
  name: "Home",

  setup() {

    

    const state = reactive({
      userinfo: JSON.parse(window.sessionStorage.getItem("user")),
      routes : computed(()=>{return store.state.routes})
    })

    const errorHandler=()=> {
      ElMessage.error("头像加载失败，请检查头像路径是否正确");
      return true;
    }
     const handleCommand = (command) =>{
      switch (command) {
        case "userinfo":
          break;
        case "setting":
          break;
        case "logout":
          ElMessageBox.confirm(
            "此操作将会注销本次登录，确认继续？",
            "提示",
            {
              confirmButtonText: "确认",
              cancelButtonText: "取消",
              type: "warning",
            }
          )
            .then(() => {
              // 向后台发送注销请求，并清空sessionStorage，跳转至登录页
              postRequest("/logout").then((res) => {
                window.sessionStorage.clear();
                // 清空vuex中的菜单列表
                store.commit("initRoutes", []);
                setTimeout(() => {
                  router.replace("/");
                }, 200);
              });
            })
            .catch(() => {
              ElMessage({
                type: "info",
                message: "取消操作",
              });
            });
          break;
      }
    }
    return {
        ...toRefs(state),
        errorHandler,
        handleCommand
    };
  } 
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-size: 14px;
}
.header-title{
  color:#FFF;
}
.home-header {
  width: 100%;
  height: min-content !important;
  background: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
}

.home-header .header-title {
  font-family: 华文行楷, serif;
  font-size: 1.8rem;
}

.home-header .userinfo {
  cursor: pointer;
  margin: 10px;
}

.home-header .userinfo img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.home-welcome {
  margin: 40px auto;
  text-align: center;
  font-family: 华文行楷, monospace;
  font-size: 1.6rem;
  color: #333;
}
.home-router-view {
  width: 100%;
  margin-top: 40px;
}
.el-dropdown-menu__item:focus,
.el-dropdown-menu__item:not(.is-disabled):hover {
  background: #666 !important;
  transition: background 0.2s;
  opacity: 0.8;
}
</style>
